package com.example.home
import androidx.core.view.get
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentPagerAdapter
import androidx.viewpager.widget.ViewPager
import com.alibaba.android.arouter.facade.annotation.Route
import com.example.common.RouterPath
import com.example.common.base.BaseFragment
import com.example.home.adapter.HomeFragmentAdapter
import com.example.home.databinding.FragmentHomeBinding
import com.example.home.fragment.DailyFragment
import com.example.home.fragment.DiscoverFragment
import com.example.home.fragment.NominateFragment
import com.google.android.material.tabs.TabLayout
import com.google.android.material.tabs.TabLayoutMediator
import dagger.hilt.android.AndroidEntryPoint
import me.majiajie.pagerbottomtabstrip.listener.OnTabItemSelectedListener


@Route(path = RouterPath.PATH_FRAGMENT_HOME)
@AndroidEntryPoint
class HomeFragment : BaseFragment<FragmentHomeBinding>() {

    private var fragments = mutableListOf<Fragment>()


    private lateinit var mPageAdapter : HomeFragmentAdapter

    override fun getLayoutId(): Int=R.layout.fragment_home

    override fun initViews() {

        //添加fragment
        fragments.clear()
        fragments.add(DiscoverFragment.newInstance())
        fragments.add(NominateFragment.newInstance())
        fragments.add(DailyFragment.newInstance())
        //设置viewPager适配器
        mPageAdapter=HomeFragmentAdapter(fragments,childFragmentManager,FragmentPagerAdapter.BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT)
        mDataBinding.viewPageHome.adapter=mPageAdapter



         //添加tab标签
        // mDataBinding.tabLayout.addTab(mDataBinding.tabLayout.newTab().setText("发现"))
        // mDataBinding.tabLayout.addTab(mDataBinding.tabLayout.newTab().setText("推荐"))
        // mDataBinding.tabLayout.addTab(mDataBinding.tabLayout.newTab().setText("最新"))


        mDataBinding.viewPageHome.addOnPageChangeListener(object : ViewPager.OnPageChangeListener{
            override fun onPageScrolled(
                position: Int,
                positionOffset: Float,
                positionOffsetPixels: Int
            ) {

            }
            override fun onPageSelected(position: Int) {
              when(position){
                  0->(fragments[position] as DiscoverFragment).lazyLoadData()
                  1->(fragments[position] as NominateFragment).lazyLoadData()
                  2->(fragments[position] as DailyFragment).lazyLoadData()
              }
            }

            override fun onPageScrollStateChanged(state: Int) {

            }
        })

        //关联起来
        mDataBinding.tabLayout.setupWithViewPager(mDataBinding.viewPageHome)
        mDataBinding.viewPageHome.offscreenPageLimit=2
        mDataBinding.viewPageHome.currentItem=1
//        mDataBinding.tabLayout.addOnTabSelectedListener(object : TabLayout.OnTabSelectedListener{
//            override fun onTabSelected(tab: TabLayout.Tab?) {
//                mDataBinding.viewPageHome.currentItem=tab!!.position
//            }
//
//            override fun onTabUnselected(tab: TabLayout.Tab?) {
//
//            }
//
//            override fun onTabReselected(tab: TabLayout.Tab?) {
//
//            }
//
//        })


    }
}